﻿using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using WiseLinkWPF.Windows.Controls.MailBar;
using CCF20Model;
namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static bool LoginSuccess = false;
        protected Button currentButton = null;

        public MainWindow()
        {
            InitializeComponent();
        }
        
        public void Button_Click(object sender, RoutedEventArgs e)
        {
            ToggleButtonStyle((Button)sender);
            if (((Button)sender).Name == "Reset")
            {
                ResetSystem();
                Utility.ShowMessageBox("系统重置成功");
            }
            else
            {
                SelectorControl.EvalValue = ((Button)sender).Name;
                CommandManager.InvalidateRequerySuggested();
                
            }
            
        }
        private void ResetSystem()
        {
            ModelFactory modelFactory = new ModelFactory();
            VehicleModel vm = (VehicleModel)modelFactory.CreateModel(ModelName.VehicleModel);
            vm.ExecuteSql(null, "delete generalstatus");
            vm.ExecuteSql(null, "delete carestats");
            vm.ExecuteSql(null, "delete clearcodehistory");
            vm.ExecuteSql(null, "delete faults");
            vm.ExecuteSql(null, "delete troubleddevice");
            
            //can't delete customer,vehicle and transferedcustomer as they are in a group
            //vm.ExecuteSql(null, "delete transferedcustomers");
            vm.ExecuteSql(null, "delete contacthistory");
            vm.ExecuteSql(null, "delete smshistory");
            vm.ExecuteSql(null, "delete vehiclemaintenancehistory");
            vm.ExecuteSql(null, "delete vehiclemaintenancerecord");
            vm.ExecuteSql(null, "delete urgentsmsqueue");
            vm.ExecuteSql(null, "delete vehiclerepairhistory");
            vm.ExecuteSql(null, "delete devices");
            vm.ExecuteSql(null, "update Vehicles set CurrentMileage=null,LastContactResult=null, LastServcieDate=null,LastServiceMileage = null,LatestActiveTime =null,BookTime=null ");
            vm.ExecuteSql(null, "update CompanyProfile set ZICUpdateTime = null");
            vm.ExecuteSql(null, "delete faults");
            vm.ExecuteSql(null, "delete customizedtroublecode");
        }
        private void LauchInStock()
        {
            ButtonAutomationPeer peer = new ButtonAutomationPeer(InStock);
            IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
            invokeProv.Invoke(); 
        }
            
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            this.tblCurrentUser.Text = (string)Application.Current.Properties["CurrentUser"];
            //this.tblCurrentUser.Width = this.tblCurrentUser.Text.Length * 10;
            //this.lblLoginName.Width = ccfMainWindow.Width - this.tblCurrentUser.Width - 10 - 570;//170 is the width of the logon, 10 is the margin between component.
            LoggerHelper.LogCCF20(string.Format("User={0} Permission={1}",this.tblCurrentUser.Text,(string)Application.Current.Properties["AllowedPermission"]));
            if (this.InStock.IsEnabled)
            {
                LauchInStock();
            }
            if (App.IsMobileBranch)
            {
                this.bar.Sections[1].IsEnabled = false;
                this.bar.Sections[2].IsEnabled = false;
                this.bar.Sections[3].IsEnabled = false;
                this.Title = this.Title + "营业厅版";
                
            }
        }

    

        private void OutlookSection_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            
        }
        private void ToggleButtonStyle(Button clickedButton)
        {
            if (currentButton != null)
            {
                currentButton.Style = (Style)FindResource("NavButtonStyle");
            }

            currentButton = clickedButton;
            currentButton.Style = (Style)FindResource("ClickedNavButton");
        }

    }
}
